package com.dabbo.starship.cell;

import java.rmi.RemoteException;

import com.dabbo.starship.interfaces.Block;
import com.dabbo.starship.interfaces.Cell;

public class CellBattery extends CellDefault {

	private static final long serialVersionUID = -5278504518954812550L;

	protected int batteryCharge	= 0;
	protected static int MAX_POWER = 175;
	protected boolean charging = false;
	protected int ticker = 0;
	
	public CellBattery(Block parent) throws RemoteException {
		super(parent);
		this.cellType = Cell.CELL_BATTERY;
		this.cargoAllowed = false;
		this.passable = true;
		this.powerOutput = 0;
		this.activated = false;
	}
	
	@Override
	public void activate() {
		this.activated = true;
	}

	@Override
	public void deActivate() {
		this.activated = false;
	}
	
	@Override
	public void setBatteryCharge(boolean charging) {
		this.charging = charging;
	}
	
	@Override
	public boolean isCharging() {
		return this.charging;
	}
	
	@Override
	public void tick() {
		ticker++;
		if (damage > 50) {
			Double test = 30 + (Math.random() * 80);
			if (test < damage) {
				this.activated = false;
				this.charging = false;
				this.powerOutput = 0;
			}
			this.batteryCharge = this.batteryCharge - new Double(2 + (Math.random() * 4)).intValue(); 
			if (this.batteryCharge < 0) {
				this.powerFailue();
			}
		}
		if (this.activated) {
			this.powerOutput = CellPower.MAX_POWER;
		}
		if (this.charging) {
			this.powerOutput = new Double(-(CellPower.MAX_POWER * 1.5)).intValue();
		}
		if (ticker > 3) {
			ticker = 0;
			if (this.activated) {
				this.batteryCharge = this.batteryCharge - new Double(5 + (Math.random() * 20)).intValue();
				if (this.batteryCharge < 0) {
					this.powerFailue();
				}
			}
			if (this.charging) {
				this.batteryCharge = this.batteryCharge + new Double(1 + (Math.random() * 10)).intValue();
				if (this.batteryCharge > 1000) {
					this.batteryCharge = 1000;
				}
			}
		}
	}

	@Override
	public void powerFailue() {
		this.activated = false;
		this.charging = false;
		this.powerOutput = 0;
	}

	@Override
	public int getBatteryCharge() throws RemoteException {
		return this.batteryCharge;
	}

}
